package com.jwdf.uda.ui.fragment.search;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.SearchInputEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.action.SearchHotAction;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.view.HotSearchLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/18.
 */
public class SearchHotFragment extends FrameLayout {
    //    FragmentHotSearchBinding mBinding;
    List<String> hotSearchList = new ArrayList<>();
    HotSearchLayout mSearchHot;
    private List<Call> requestCalls;
    boolean mShowHistoryTitle;


    public SearchHotFragment(Context context, boolean showTitle) {
        super(context);
        this.mShowHistoryTitle = showTitle;
        LayoutInflater.from(getContext()).inflate(R.layout.fragment_hot_search, this, true);
        requestData();
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_hot_search, container, false);
//        requestData();
//        return mBinding.getRoot();
//    }


//    public static SearchHotFragment instance() {
//        SearchHotFragment view = new SearchHotFragment();
//        return view;
//    }

    public void initViews(final List<String> hotSearchList) {
        mSearchHot = (HotSearchLayout) findViewById(R.id.hsl_hotsearch);
        mSearchHot.setText(hotSearchList);
        showHistoryTitle(mShowHistoryTitle);
//        mBinding.hslHotsearch.setText(hotSearchList);
        mSearchHot.setOnTextItemClickListener(new HotSearchLayout.OnTextItemClickListener() {
            @Override
            public void onTextClick(int index, LinearLayout linearLayout) {
                EventBusUtil.post(new SearchInputEvent(hotSearchList.get(index)));
            }
        });
    }

    public void showHistoryTitle(boolean showTitle){
        mSearchHot.setHistoryTitleVisibility(showTitle ? VISIBLE : GONE);
    }

    protected void requestData() {
        onNewRequestCall(SearchHotAction.newInstance(getContext())
                .request(new AHttpService.IResCallback<PbBean.PbResPageHotSearch>() {
                    @Override
                    public void onCallback(int resultCode, Response<PbBean.PbResPageHotSearch> response, PbBean.PbBaseServerInfo
                            baseServerInfo) {
                        if (response.body() != null) {
                            doSuccessResponse(response.body());
                        }
                    }
                }));
    }

    private void doSuccessResponse(PbBean.PbResPageHotSearch body) {
        cancelRequest();
        hotSearchList.clear();
        for (int i = 0; i < body.getHotSearchListCount(); i++) {
            hotSearchList.add(body.getHotSearchList(i));
        }
        initViews(hotSearchList);
    }

    public void onNewRequestCall(Call call) {
        addRequestCall(call);
    }

    private void addRequestCall(Call call) {
        if (requestCalls == null) {
            requestCalls = new ArrayList<>();
        }
        requestCalls.add(call);
    }

    public void cancelRequest() {
        if (requestCalls == null)
            return;
        for (int i = 0; i < requestCalls.size(); i++) {
            Call call = requestCalls.get(i);
            if (call == null)
                return;
            if (!call.isCanceled()) {
                call.cancel();
            }
        }
        requestCalls.clear();
    }
}
